package com.demo.config.canal.test3;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

/**
 * 项目名称: 类名: SpringContextUtil 描述： 获取bean的工具类，可用于在线程里面获取bean 创建人: awsm 创建时间: Dec 17, 2015
 * 10:46:44 PM 修改人：little evil 修改时间：May 18, 2018 04:01:34 PM
 * 修改备注：添加getActiveProfile方法,获取当前环境 版本：1.1
 */
@Component
public class SpringContextUtil implements ApplicationContextAware {

	private static ApplicationContext context = null;

	/*
	 * (non Javadoc)
	 *
	 * @Title: setApplicationContext
	 *
	 * @Description: spring获取bean工具类
	 *
	 * @param applicationContext
	 *
	 * @throws BeansException
	 *
	 * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.
	 * springframework.context.ApplicationContext)
	 */
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		context = applicationContext;
	}

	// 传入线程中
	public static <T> T getBean(String beanName) {
		return (T) context.getBean(beanName);
	}

	/// 获取当前环境
	public static String getActiveProfile() {
		Environment environment = context.getEnvironment();
		String[] activeProfiles = environment.getActiveProfiles();
		return activeProfiles[0];
	}

}
